--
-- @Author:      qinxugao
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的派发

local skynet = require "skynet"
local log = require "Log"
local md5 = require "md5"
local crypt = require "crypt"
local cjson = require "cjson"
local queue = require "skynet.queue"

local AssertEx = AssertEx

local Message = class("Message")
---------------------------------------------------------
-- Private
---------------------------------------------------------
function Message:ctor()
	self.tbSelfMsg = {} --本服务要监听的消息
	self.mqueue = queue()

end

--注册本服务里的消息
function Message:registerSelf(msgName,callback)
	self.tbSelfMsg[msgName] = callback
end

--消息进行队列化
function Message:queueMessage(session, source, cmd, ...)		
	local func = handler(self,self.dispatchMessage)
	local resut = self.mqueue(func,session, source, cmd, ...)
	return result
end	

--消息派发
function Message:dispatchMessage(session, source, cmd, ... )
	-- print("####dispatchMessage#######",source, cmd, ...)
	local ok, res
	local func = self.tbSelfMsg[cmd] -- gate是否有handler
	if not func then 
		log.error("####### cmd "..cmd .." not found at manager_service ")
		return
	end
	if cmd == "socket" then 			
		skynet.retpack(xx_pcall(func, source,...))	
		return
	end
	skynet.retpack(xx_pcall(func, ...))
end

function Message:dispatch()
	return handler(self,self.queueMessage)
end


return Message